/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Br.Com.Cds.Academia.Exercicio;

import Br.Com.Cds.Academia.Banco.BancoBean;
import Br.Com.Cds.Academia.DAO.AcessoMySQL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Pedro
 */
public class ExercicioControl {
    
    PreparedStatement pstm;
    ResultSet rs;
    
    String consultaexercicio = "SELECT * FROM EXERCICIO WHERE DESCRICAO LIKE ?";
    String cadastraexercicio = "INSERT INTO EXERCICIO (DESCRICAO,GRUPOMUSCULAR,STATUS,CAMINHOVIDEO)VALUES (?,?,?,?)";
    String alterarexercicio = "UPDATE EXERCICIO SET  DESCRICAO=?,GRUPOMUSCULAR=?,STATUS=?, CAMINHOVIDEO=? WHERE IDEXERCICIO=?";
    String excluirexercicio = "DELETE FROM EXERCICIO WHERE IDEXERCICIO=?"; 
    
    
    
    public void cadastraExercicio (ExercicioBean exercicio){
        try {
            AcessoMySQL mysql = new AcessoMySQL();
            pstm = (PreparedStatement)mysql.conectar().prepareStatement(cadastraexercicio);
            pstm.setString(1,exercicio.getDescricao() );
            pstm.setString(2,exercicio.getGrupomuscular());
            pstm.setString(3,exercicio.getStatus());
            pstm.setString(4,exercicio.getCaminhovideo());
            pstm.executeUpdate();
            mysql.desconectar();
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "SQL EXCEPTION ! ERRO AO TENTAR CADASTRAR UM EXERCICIO!","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
       }
        
    }
    
    
    public void alterarExercicio (ExercicioBean exercicio){
        
        try {
            AcessoMySQL mysql = new AcessoMySQL();
            pstm = (PreparedStatement) mysql.conectar().prepareStatement(alterarexercicio);
            pstm.setString(1, exercicio.getDescricao());
            pstm.setString(2, exercicio.getGrupomuscular());
            pstm.setString(3, exercicio.getStatus());
            pstm.setString(4, exercicio.getCaminhovideo());
            pstm.setInt(5, exercicio.getIdexercicio());
            pstm.executeUpdate();
            mysql.desconectar();
            
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "SQL EXCEPTION ! ERRO AO TENTAR CADASTRAR UM EXERCICIO!","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
        }
        
    }
   
    
     public List <ExercicioBean> listarExercicio (String nomeExercicio){
         List <ExercicioBean> exercicio = new ArrayList ();
         
         try {
             AcessoMySQL mysql = new AcessoMySQL();
             pstm = (PreparedStatement) mysql.conectar().prepareStatement(consultaexercicio);
             pstm.setString(1,nomeExercicio );
             rs = pstm.executeQuery();
             ExercicioBean exer;
             
             while (rs.next()){
                exer = new ExercicioBean ();
                exer.setIdexercicio(rs.getInt("IDEXERCICIO"));          
                exer.setDescricao(rs.getString("DESCRICAO"));  
                exer.setGrupomuscular(rs.getString("GRUPOMUSCULAR")); 
                exer.setStatus(rs.getString("STATUS")); 
                exer.setCaminhovideo(rs.getString("CAMINHOVIDEO"));          
                exercicio.add(exer);  
             }    
      } catch (Exception e) {
        e.printStackTrace();
         JOptionPane.showMessageDialog(null,"SQL EXCEPTION - ERRO AO TENTAR REALIZAR UMA CONSULTA","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
        
      }
        return exercicio;
      
  }
    
    
     
      public void excluirExercicio (int idexercicio){
      
      try {
          AcessoMySQL mysql = new AcessoMySQL();
          pstm = (PreparedStatement) mysql.conectar().prepareStatement(excluirexercicio);
          pstm.setInt(1,idexercicio);
          pstm.executeUpdate();
          mysql.desconectar();
            
      } catch (Exception e) {
          e.printStackTrace();
          JOptionPane.showMessageDialog(null,"SQL EXCEPTION - ERRO AO TENTAR EXCLUIR O BANCO","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
      }
      
      
      
      
  }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
